Page({
  /**
   * 页面的初始数据
   */
  data: {
    indexList: ['科创标签', '认定级别', '所属区域', '所属行业', '所属产业', '经营状态', '企业规模', '评分区间'],
    origList: [{
        title: "科创标签",
        value: [],
        checked: false,
        multiple: true,
        children: [],
        showAll: false,
        key: 'labelNameList'
      },
      {
        title: "认定级别",
        value: [],
        checked: false,
        multiple: false,
        children: [],
        showAll: true,
        key: 'lvlList'
      },
      {
        title: "所属区域",
        value: [],
        checked: false,
        multiple: false,
        children: [],
        showAll: true,
        key: "qyList"
      },
      {
        title: "所属行业",
        value: [],
        checked: false,
        multiple: false,
        children: [],
        showAll: true,
        key: 'hyList'
      },
      {
        title: "所属产业",
        value: [],
        checked: false,
        multiple: true,
        children: [],
        showAll: true,
        key: 'cyList'
      },
      {
        title: "经营状态",
        value: [],
        checked: false,
        multiple: false,
        children: [],
        showAll: true,
        key: 'entstatusList'
      },
      {
        title: "企业规模",
        value: [],
        checked: false,
        multiple: false,
        children: [],
        showAll: true,
        key: 'scaleList'
      },
      {
        title: "评分区间",
        value: [],
        checked: false,
        multiple: false,
        children: [],
        showAll: false,
        startScore: '',
        endScore: '',
      },
    ],
    useList: [],
  },

  /**
   * 方法集合 
   */
  async initCydn() {
    const params = {
      username: 'admin',
      platform: "miniApp"
    }
    const res = await getApp().globalData.request_cydn(
      '/loginByUserName', params,
      "POST"
    )
    if (res.code == 200) {
      console.log(res, '产业大脑登录信息');
      wx.setStorageSync('token_cydn', res.token)
      const allRes = await Promise.all([this.getSearchItem(), this.getIndustryCo(), this.queryMainPageSeachItem(), this.getLabAndCount()])
      console.log(allRes, 'allRes');
      if (allRes[0].labelNameList && allRes[0].labelNameList.length > 0) {
        this.data.origList[0].children = allRes[0].labelNameList.filter(val => val !== '其他').map((item) => {
          return {
            value: item,
            label: item,
            disabled: false,
            num: allRes[3].find(po => po.dataKey === item).dataValue
          };
        });
      }
      if (allRes[0].lvlList && allRes[0].lvlList.length > 0) {
        this.data.origList[1].children = allRes[0].lvlList.filter(val => val !== '其他').map((item) => {
          return {
            value: item,
            label: item,
            disabled: false,
            num: allRes[3].find(po => po.dataKey === item).dataValue
          };
        });
      }
      if (allRes[0].qyList && allRes[0].qyList.length > 0) {
        this.data.origList[2].children = allRes[0].qyList.filter(val => val !== '其他').map((item) => {
          return {
            value: item,
            label: item,
            disabled: false,
            num: allRes[3].find(po => po.dataKey === item).dataValue
          };
        });
      }
      if (allRes[1] && allRes[1].length > 0) {
        this.data.origList[3].children = allRes[1].filter(val => val.industryco !== '其他').map((item) => {
          return {
            value: item.industryco,
            label: item.industryco,
            disabled: false,
            num: allRes[3].find(po => po.dataKey === item.industryco).dataValue
          };
        });
      }
      if (allRes[2].pnameList && allRes[2].pnameList.length > 0) {
        this.data.origList[4].children = allRes[2].pnameList.filter(val => val !== '其他').map((item) => {
          return {
            value: item,
            label: item,
            disabled: false,
            num: allRes[3].find(po => po.dataKey === item).dataValue
          };
        });
      }
      if (allRes[0].entstatusList && allRes[0].entstatusList.length > 0) {
        this.data.origList[5].children = allRes[0].entstatusList.filter(val => val !== '其他').map((item) => {
          return {
            value: item,
            label: item,
            disabled: false,
            num: allRes[3].find(po => po.dataKey === item).dataValue
          };
        });
      }
      if (allRes[0].scaleList && allRes[0].scaleList.length > 0) {
        let arr = allRes[0].scaleList;
        const uniqueArr = [...new Set(arr)];
        this.data.origList[6].children = uniqueArr.filter(val => val !== '其他').map((item) => {
          return {
            value: item,
            label: item,
            disabled: false,
            num: allRes[3].find(po => po.dataKey === item).dataValue
          };
        });
      }
      this.setData({
        origList: this.data.origList,
        useList: JSON.parse(JSON.stringify(this.data.origList))
      })
      console.log(this.data.useList, 'useListuseListuseListuseList');
    }
  },
  searchInput(e) {
    const theOne = this.data.useList.find(pre => pre.title === e.currentTarget.dataset.title)
    if (e.currentTarget.dataset.value === 'end') {
      theOne.endScore = e.detail.value
    } else {
      theOne.startScore = e.detail.value
    }
    this.setData({
      useList: this.data.useList
    })
  },
  goConfirm() {
    const theOne = this.data.useList.find(pre => pre.title === '评分区间')
    if (theOne.endScore || theOne.startScore) {
      if (+theOne.startScore > +theOne.endScore) {
        wx.showToast({
          title: '请合理输入科创评分区间值',
          icon: 'none'
        })
        return
      }
    }
    let obj = {}
    let labelList = []
    this.data.useList.forEach(pre => {
      if (pre.title === '评分区间') {
        obj.startScore = pre.startScore
        obj.endScore = pre.endScore
        if (obj.endScore) {
          labelList.push(`评分区间：${obj.kcScoreMin}-${obj.kcScoreMax}`)
        }
      } else {
        obj[pre.key] = pre.value
          if (pre.value.length > 0) {
            labelList.push(`${pre.title}：${pre.value.join('、')}`)
          }
      }
    })
    wx.setStorageSync('kc_filter', obj)
    wx.setStorageSync('kc_labelList', labelList)
    wx.navigateTo({
      url: '/packageA/pages/homelink/measure_limit_search3/measure_limit_search3'
    })
  },
  toChange(e) {
    const title = e.currentTarget.dataset.title
    const theOne = this.data.useList.find(val => val.title === title)
    theOne.checked = !theOne.checked
    if (theOne.checked) {
      theOne.value = theOne.children.map(pre => pre.value)
    } else {
      theOne.value = []
    }
    this.setData({
      useList: this.data.useList
    })
  },
  selectOne(e) {
    const title = e.currentTarget.dataset.title
    const value = e.currentTarget.dataset.value
    const theOne = this.data.useList.find(val => val.title === title)
    const fOneIndex = theOne.value.findIndex(pre => pre === value)
    if (theOne.multiple) {
      if (fOneIndex > -1) {
        theOne.value.splice(fOneIndex, 1)
      } else {
        theOne.value.push(value)
      }
    } else {
      if (fOneIndex > -1) {
        theOne.value = []
      } else {
        theOne.value = []
        theOne.value.push(value)
      }
    }
    if (theOne.value.length === theOne.children.length) {
      theOne.checked = true
    } else {
      theOne.checked = false
    }
    this.setData({
      useList: this.data.useList
    })
  },
  // const res = await getSearchItem({});
  // const result = await getSearchItem1({});
  // const chanye = await queryMainPageSeachItem({});
  async getSearchItem() {
    const res = await getApp().globalData.request_cydn('/kcorpInfo/industry/getSearchItem', {},
      "POST")
    return res.data
  },
  async getIndustryCo() {
    const res = await getApp().globalData.request_cydn('/kcorpInfo/industry/getIndustryCo', {},
      "POST")
    return res.data
  },
  async queryMainPageSeachItem() {
    const res = await getApp().globalData.request_cydn('/industry/dnBase/queryMainPageSeachItem', {},
      "POST")
    return res.data
  },
  async getLabAndCount() {
    const res = await getApp().globalData.request_cydn('/kcorpInfo/industry/getLabAndCount', {},
      "POST")
    return res.data
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.initCydn()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})